package com.seh.contentcenter.controller;

import com.seh.contentcenter.domain.Result;
import com.seh.contentcenter.dto.GoodsDTO;
import com.seh.contentcenter.entity.Goods;
import com.seh.contentcenter.service.Impl.GoodsServiceImpl;
import com.seh.contentcenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsServiceImpl goodsService;

    /**
     * 查询所有商品
     *
     * @return
     */
    @GetMapping("/findAll/{pageIndex}")
    public Result<List<GoodsDTO>> findAll(@PathVariable Integer pageIndex) {
        return ResultUtil.goodsInfo(goodsService.findAll(pageIndex));
    }

    /**
     * 模糊查询商品
     *
     * @param keyword
     * @return
     */
    @GetMapping("/findByName")
    public Result<List<Goods>> findByName(@RequestParam String keyword) {
        return ResultUtil.goodsInfo(goodsService.findByName(keyword));
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result<GoodsDTO> findById(@PathVariable Integer id) {
        GoodsDTO goodsDTO = goodsService.findById(id);
        return ResultUtil.goodsInfo(goodsDTO);
    }

    /**
     * 新增商品
     *
     * @param goods
     * @return
     */
    @PostMapping("/addNewGoods")
    public String addNewGoods(@RequestBody Goods goods) {
        String s = goodsService.addNewGoods(goods);
        return s;
    }

    /**
     * 根据用户id，查询该用户发布的二手商品
     *
     * @param userId
     * @param pageIndex
     * @return
     */
    @GetMapping("/findAllGoodsByUserId")
    public Result<List<GoodsDTO>> findAllGoodsByUserId(@RequestParam("userId") Integer userId,
                                                       @RequestParam("pageIndex") Integer pageIndex) {
        return ResultUtil.goodsInfo(goodsService.findAllGoodsByUserId(userId, pageIndex));
    }

    /**
     * 逻辑删除商品
     *
     * @param goodsId
     */
    @GetMapping("/deleteGoods/{goodsId}")
    public void deleteGoods(@PathVariable Integer goodsId) {
        goodsService.deleteGoods(goodsId);
    }

    @GetMapping("/findAllGoodsByAddress")
    public Result<List<GoodsDTO>> findAllGoodsByAddress(@RequestParam("addressName") String addressName,
                                                        @RequestParam("price") String price,
                                                        @RequestParam("publishTime") String publishTime,
                                                        @RequestParam("pageIndex") Integer pageIndex) {
        return ResultUtil.goodsInfo(goodsService.findAllGoodsByAddress(addressName, price, publishTime, pageIndex));
    }
}
